/*
 * This class will be called when user select history menu. here user will see all the variable names in list for which he 
 * can see history graph of that variable. If user select variable dialog will be appear in which user will enter year, 
 * month, day and no of days. Then it will show that day to no of days data in the graph.
 */
package com.fipl.taksar;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HistoricalActivity extends Activity {

	private EditText yearEditText;			// mapped with xml file edit text
	private EditText monthEditText;			// mapped with xml file edit text	
	private EditText dayEditText;			// mapped with xml file edit text
	private EditText noofDaysEditText;		// mapped with xml file edit text
	private boolean yearFlag = false;		// will check year edit box is filled or not
	private boolean monthFlag = false;		// will check month edit box is filled or not
	private boolean dayFlag = false;		// will check day edit box is filled or not
	private boolean noofDaysFlag = false;	// will check no of month edit box is filled or not
	private String year="";					// will store enter year
	private String month="";				// will store enter month
	private String day="";					// will store enter day
	private String noofDays="";				// will store enter no of days
	private int currentYear;				// this will store current year value
//	private int currentMonth;				// this will store current month value
	private int variableSelect = 0;			
	private LinearLayout linearLayout;
//	private int height;						// this will store default height of the device
	private ListView listView;				// this will contain list of views on the device
	private String [] variable={"Temperature_1", "Temperature_2", "Temperature_3" ,"Temperature_4", 
			"Humidity_1", "Humidity_2", "Humidity_3","Humidity_4","PSTemperature_1","PSTemperature_2",
			"Diesel_1","Diesel_2",
			"Voltage_1A","Voltage_1B","Voltage_1C","Voltage_2A","Voltage_2B","Voltage_2C",
			"Voltage_3A","Voltage_3B","Voltage_3C","Voltage_4A","Voltage_4B","Voltage_4C",
			"Voltage_5A","Voltage_5B","Voltage_5C","Voltage_6A","Voltage_6B","Voltage_6C",
			"Voltage_7A","Voltage_7B","Voltage_7C","Voltage_8A","Voltage_8B","Voltage_8C",
			"Current_1A","Current_1B","Current_1C","Current_2A","Current_2B","Current_2C",
			"Current_3A","Current_3B","Current_3C","Current_4A","Current_4B","Current_4C",
			"Current_5A","Current_5B","Current_5C","Current_6A","Current_6B","Current_6C",
			"DigitalInput_1","DigitalInput_2","DigitalInput_3","DigitalInput_4","DigitalInput_5",
			"DigitalInput_6","DigitalInput_7","DigitalInput_8","DigitalInput_9","DigitalInput_10",
			"DigitalInput_11","DigitalInput_12","DigitalInput_13","DigitalInput_14","DigitalInput_15",
			"DigitalInput_16","DigitalInput_17","DigitalInput_18","DigitalInput_19","DigitalInput_20",
			"DigitalInput_21","DigitalInput_22","DigitalInput_23","DigitalInput_24","DigitalOutput_1",
			"DigitalOutput_2","DigitalOutput_3","DigitalOutput_4","DigitalOutput_5","DigitalOutput_6",
			"DigitalOutput_7","DigitalOutput_8","DigitalOutput_9","DigitalOutput_10","DigitalOutput_11",
			"DigitalOutput_12","DigitalOutput_13","DigitalOutput_14","DigitalOutput_15","DigitalOutput_16"
	};

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.variable);
		
		// get device default screen size
//		Display display = getWindowManager().getDefaultDisplay();
//		height = display.getHeight();								// get default height of the device

		Date date = new Date();										// get current date 
		currentYear = (date.getYear()+1900);						// get current year
//		currentMonth = (date.getMonth()+1);							// get current month
		getData();
	}
	
	/*
	 * This method will show all the variable list and when user select particular variable then
	 * it will called showdialog method from which user will enter year, month, day, and no of days.
	 */

	public void getData(){
		setContentView(R.layout.variable);

		linearLayout = (LinearLayout)findViewById(R.id.middleVariable);
		
		listView = new ListView(this);
		listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

		MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this,variable);
		listView.setAdapter(myArrayAdapter);
		listView.setFocusable(true);
		listView.setBackgroundColor(Color.WHITE);
		listView.setCacheColorHint(Color.TRANSPARENT);
		listView.setVerticalScrollBarEnabled(false);
		
		/*
		 * 	this will called when user select one variable from the list 
		 */
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				// TODO Auto-generated method stub
				variableSelect = arg2;
				showDialogBox(arg2);
			}
		});
		linearLayout.addView(listView);
	}
	
	/*
	 * this will set custom list view.
	 */
	public class MyArrayAdapter extends ArrayAdapter<String> {

		private final Activity context;
		private final String[] variable1;

		public MyArrayAdapter(Activity context, String[] variable) {
			super(context, R.layout.textlist, variable);
			this.context=context;
			this.variable1=variable;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = context.getLayoutInflater();				// get layout inflater to inflate XML file.
			View rowView = inflater.inflate(R.layout.textlist, null, true);		// inflate view using inflater.
			TextView variableTextView = (TextView) rowView.findViewById(R.id.equipotxt);	// mapped text view with the text view define in XML. 
			variableTextView.setText(variable1[position]);					// set variable name in the textview
			return rowView;
		}

	}

	/*
	 * this method is called when user select particular variable and user can enter year month day and no of days,
	 * this dialog have two button show report and cancel. cancel will cancel the dialog and user stay on the current page while 
	 * show report will take user to new activity and show graph for enter detail. 
	 */
	public void showDialogBox(final int args){
		
		AlertDialog.Builder builder;
		AlertDialog dialog;
		LayoutInflater inflater = (LayoutInflater)HistoricalActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.historydialog, (ViewGroup)findViewById(R.id.mainDlglayout));

		yearEditText = (EditText)view.findViewById(R.id.edtYear);		// edit text for year
		yearEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
		monthEditText = (EditText)view.findViewById(R.id.edtMonth);		// edit text for month
		monthEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
		dayEditText = (EditText)view.findViewById(R.id.edtDay);			// edit text for day
		dayEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
		noofDaysEditText = (EditText)view.findViewById(R.id.edtnoDays);	// edit text for no of days
		noofDaysEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
		
		if(year.length()!=0){
			yearEditText.setText(year);
		}
		
		if(month.length()!=0){
			monthEditText.setText(month);
		}
		
		if(day.length()!=0){
			dayEditText.setText(day);
		}
		
		if(noofDays.length()!=0){
			noofDaysEditText.setText(noofDays);
		}

		builder = new AlertDialog.Builder(HistoricalActivity.this);
		builder.setView(view);
		builder.setTitle("Variable " +variable[(args)]);

		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});

		builder.setPositiveButton("Show Report", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				year = yearEditText.getText().toString();				// get enter string in year edit text
				month = monthEditText.getText().toString();				// get enter string in month edit text
				day = dayEditText.getText().toString();					// get enter string in day edit text
				noofDays = noofDaysEditText.getText().toString();		// get enter string in no of days edit text
				
				/*
				 * all the condition that any of the above filed is non empty. if any of them is empty then it will not go to next page
				 * user will get warning and dialog stays on the screen.
				 */
				
				if(year.length()!=0){
					if(Integer.parseInt(year)>currentYear | Integer.parseInt(year)==0 | Integer.parseInt(year)<2005  ){
						Toast.makeText(getApplicationContext(), "Please enter year between 2005 to "+ currentYear + " !! ", Toast.LENGTH_SHORT).show();
					}else{
//						Log.d("System out", "Year is "+Integer.parseInt(year));
						if(year.length()==4){
							yearFlag = true;
						}else {
							Toast.makeText(getApplicationContext(), "Please enter correct year!!", Toast.LENGTH_SHORT).show();
						}
					}
				} else {
					Toast.makeText(getApplicationContext(), "Please enter year!!", Toast.LENGTH_SHORT).show();
				}
				
				if(month.length()!=0){
					if(Integer.parseInt(month)>12 | Integer.parseInt(month)==0){
						Toast.makeText(getApplicationContext(), "Please enter correct month!!", Toast.LENGTH_SHORT).show();
					}else{
						monthFlag = true;
					}
				}else{
					Toast.makeText(getApplicationContext(), "Please enter month!!", Toast.LENGTH_SHORT).show();
				}
				
				if(day.length()!=0){
					if(Integer.parseInt(day)>31 | Integer.parseInt(day)<=0){
						Toast.makeText(getApplicationContext(), "Please enter correct day!!", Toast.LENGTH_SHORT).show();
					}else{
						dayFlag = true;
					}
				}else{
					Toast.makeText(getApplicationContext(), "Please enter day!!", Toast.LENGTH_SHORT).show();
				}
				
				if(noofDays.length()!=0){
					if(Integer.parseInt(noofDays)<=0){
						Toast.makeText(getApplicationContext(), "Please enter valid number of Days!!", Toast.LENGTH_SHORT).show();
					}else{
						noofDaysFlag = true;
					}
					
				}else{
					Toast.makeText(getApplicationContext(), "Please enter Number of Days!!", Toast.LENGTH_SHORT).show();
				}
				// if all the field r properly enter and data are correct then only shownext method will be called.
				if(yearFlag & monthFlag & dayFlag & noofDaysFlag){
					ShowNext();
				}else{
					yearFlag = false;
					monthFlag = false;
					dayFlag = false;
					noofDaysFlag = false;
					showDialogBox(args);
				}
			}
		});
		dialog = builder.create();
		dialog.show();
	}

	/*
	 * this method will wrap all the enter data in to the bundle and send to new activity showgraph.
	 */
	public void ShowNext(){
		Bundle bundle = new Bundle();
		bundle.putString("year", year);
		bundle.putString("month", month);
		bundle.putString("day", day);
		bundle.putString("noofdays", noofDays);
		bundle.putInt("variableIndex", variableSelect);
		bundle.putString("variable", variable[variableSelect]);
		Intent intent = new Intent();
		intent.putExtras(bundle);
		intent.setClass(getApplicationContext(), ShowGraph.class);
		startActivityForResult(intent, 2);
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	public void onStop(){
		super.onStop();
		HistoricalActivity.this.finish();
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	public boolean onCreateOptionsMenu (Menu menu){
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.option, menu);
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	public boolean onOptionsItemSelected(MenuItem menuItem){
		
		switch (menuItem.getItemId()) {
		
		case R.id.menuDb:
			((GlobalData)this.getApplicationContext()).setLoginFlag(false);
			((GlobalData)this.getApplicationContext()).setMenuNo(100);
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), ConfigurationActivity.class);
			startActivity(intent);
			break;
		
		case R.id.menuExit:
			HistoricalActivity.this.finish();
			break;
		
		}
		return true;
	}
	
}
